package com.sp.model.entity;

import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.http.HttpStatus;

import java.io.Serializable;

/**
 * 请求统一返回结果
 *
 * @param <T>
 * @author tong
 */
@Data
@NoArgsConstructor
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 请求是否成功
     */
    private boolean flag = false;

    /**
     * 状态码
     */
    private Integer code;

    /**
     * 响应信息
     */
    private String message;

    /**
     * 响应数据
     */
    private T data;

    /**
     * public <泛型类型占位参数> 返回类型 方法名（类型参数 变量名）
     */
    public static <T> Result<T> success(String message) {
        Result<T> result = new Result<>();
        result.flag = true;
        result.code = HttpStatus.OK.value();
        result.message = message;
        return result;
    }

    public static <T> Result<T> success(String message, T object) {
        Result<T> result = new Result<>();
        result.flag = true;
        result.code = HttpStatus.OK.value();
        result.message = message;
        result.data = object;
        return result;
    }

    public static <T> Result<T> success(String message, Integer code) {
        Result<T> result = new Result<>();
        result.flag = true;
        result.code = code;
        result.message = message;
        return result;
    }

    public static <T> Result<T> success(String message, Integer code, T object) {
        Result<T> result = new Result<>();
        result.flag = true;
        result.code = code;
        result.message = message;
        result.data = object;
        return result;
    }

    public static <T> Result<T> error(String message) {
        Result<T> result = new Result<>();
        result.code = HttpStatus.BAD_REQUEST.value();
        result.message = message;
        return result;
    }

    public static <T> Result<T> error(String message, Integer code) {
        Result<T> result = new Result<>();
        result.code = code;
        result.message = message;
        return result;
    }

}
